package cycling.taxi.com.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import cycling.taxi.com.IConstants;
import cycling.taxi.com.R;
import cycling.taxi.com.bean.User;
import cycling.taxi.com.dialog.ActionSheetDialog;
import cycling.taxi.com.dialog.LoadingDialog;
import cycling.taxi.com.entity.Repair;

import cycling.taxi.com.person.ExitApplication;
import cycling.taxi.com.request.Config;
import cycling.taxi.com.request.RM;
import cycling.taxi.com.request.RequestBean;
import cycling.taxi.com.request.RequestCallback;
import cycling.taxi.com.request.RequestParams;
import cycling.taxi.com.request.RequestUtil;
import cycling.taxi.com.utils.LogUtil;
import cycling.taxi.com.utils.MD5Util;
import cycling.taxi.com.utils.PhoneUtils;
import cycling.taxi.com.utils.RoundBitmap;
import cycling.taxi.com.utils.SPUtil;
import cycling.taxi.com.utils.ToastUtil;
import cycling.taxi.com.utils.Urls;

/**
 * 登录
 * Created by Ytw on 2017/5/26.
 */
public class LoginAty extends AppCompatActivity implements View.OnClickListener, RequestCallback {
    @InjectView(R.id.aty_login_username)
    EditText mUsername;
    @InjectView(R.id.aty_login_password)
    EditText mPassword;
    @InjectView(R.id.aty_login_register)
    TextView mRegister;
    @InjectView(R.id.aty_login_forgetpawd)
    TextView mForgetpawd;
    @InjectView(R.id.aty_login_enter)
    Button mEnter;
    @InjectView(R.id.aty_login_imag)
    ImageView mImag;
    //测试按钮
    @InjectView(R.id.aty_login_enter_test)
    Button mEnterTest;
//    @InjectView(R.id.cb)
//    CheckBox cb;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
            initUI();
            initListener();
    }

    private void initListener() {
        mRegister.setOnClickListener(this);
        mEnter.setOnClickListener(this);
        mForgetpawd.setOnClickListener(this);
        mEnterTest.setOnClickListener(this);//测试监听
    //    cb.setOnClickListener(this);
    }

    private void initUI() {
        setContentView(R.layout.aty_login);
        ExitApplication.getInstance().addActivity(this);
        ButterKnife.inject(this);
    /*    if(SPUtil.getInstance().getData("remember").equals("true")){
            cb.setChecked(true);
            String beanJson=SPUtil.getInstance().getData("beanJson");
            User user = new Gson().fromJson(beanJson,User.class);
            mUsername.setText(user.getPhone());
            mPassword.setText(SPUtil.getInstance().getData("password"));

        }else {
            cb.setChecked(false);
        }*/

        Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.cycling_icon);
        mImag.setImageBitmap(RoundBitmap.toRoundBitmap(bitmap));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.aty_login_register:
                //注册
                startToRegister();
            break;

            case R.id.aty_login_forgetpawd:
                //忘记密码
                startIntextAty(this,ForgetPasswordActivity.class);
            break;

            case R.id.aty_login_enter:
                //登录
                startToHomeAty();
            break;
            case R.id.aty_login_enter_test:
                //登录测试
                startIntextAty(this,HomeAty.class);
                finish();
                break;
//            case R.id.cb:
//                check(v);

        }

    }
    //判断checkbox选中状态
    private void check(View v){
        if(((CheckBox) v).isChecked()){
            SPUtil.getInstance().setData("remember","true");
        }
        if(!((CheckBox) v).isChecked()){
            SPUtil.getInstance().setData("remember","false");
        }
    }
    //登录逻辑与校验
    private void startToHomeAty() {
        if (TextUtils.isEmpty(getUserName())){
            ToastUtil.getInstance().toast("请输入手机号");
            return;
        }else if(TextUtils.isEmpty(getPassword())){
            ToastUtil.getInstance().toast("密码不能为空");
            return;
        }else if (!PhoneUtils.isMobileNO(getUserName())) {
            ToastUtil.getInstance().toast("手机号码不正确");
            return;
        }
        RequestParams params = new RequestParams.Builder()
                .putParam("mobile", getUserName())
                .putParam("password", MD5Util.MD5Encode(getPassword()))
                .build();
        //开始请求发送数据到服务器服务器
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        String url = Urls.getLogin();//回去api路径

        RequestUtil.requestData(requestQueue,
                IConstants.LOGIN,
                url,
                RM.GET,
                RequestBean.class,
                params,
                this);
    }

    private String getPassword() {
        return mPassword.getText().toString().trim();
    }

    private String getUserName() {
       return mUsername.getText().toString().trim();
    }

    private void startToRegister() {
        startIntextAty(this,RegisterAty.class);
    }


    private void startIntextAty(Context context,Class cs) {
        Intent intent = new Intent(context, cs);
        startActivity(intent);
    }

    /**
     * 请求成功
     * @param bean
     * @param config
     */
    @Override
    public void requestSuccess(RequestBean bean, Config config) {
            LogUtil.e("requestSuccess数据="+bean.beanJson);


        if (bean.getStatusCode()==2){
            ToastUtil.getInstance().toast("账号不存在");
            return;
        }
        if (bean.getStatusCode()==1){
            ToastUtil.getInstance().toast("密码错误");
            return;
        }
        String []userdata=bean.beanJson.split("=");
        LogUtil.e("用户："+userdata[0]+userdata[1]);
        //存储用户信息
        SPUtil.getInstance().setData("beanJson",userdata[0]);
        //存储车辆信息
        SPUtil.getInstance().setData("verhicledata",userdata[1]);
        //存储密码
        SPUtil.getInstance().setData("password",getPassword());

        //跳转到主界面
        startIntextAty(this,HomeAty.class);
        finish();


    }

    /**
     * 请求失败
     * @param bean
     * @param config
     */
    @Override
    public void requestFail(RequestBean bean, Config config) {
        LogUtil.e("requestFail数据="+bean.beanJson);
        LogUtil.e("requestFail");
        ToastUtil.getInstance().toast("登录失败");

    }

    /**
     * Token失效
     * @param requestTag
     * @param config
     */
    @Override
    public void requestTokenInvalid(String requestTag, Config config) {
        LogUtil.e("requestTokenInvalid");
        ToastUtil.getInstance().toast("登录失败");

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
